#ifndef MD2_ENTITY_LOADER_H
#define MD2_ENTITY_LOADER_H

#define MD2_CHECK 844121161
#define MD2_VERSION 8

#include <modules\graphics\animated_entity.h>
#include <string>

using namespace std;

namespace DelGE
{
	namespace Modules
	{
		namespace Graphics
		{
			class MD2EntityLoader
			{
			private:
				typedef float vec3_t[3];

				/* MD2 header */
				struct md2_header_t
				{
					int ident;                  /* magic number: "IDP2" */
					int version;                /* version: must be 8 */

					int skinwidth;              /* texture width */
					int skinheight;             /* texture height */

					int framesize;              /* size in bytes of a frame */

					int num_skins;              /* number of skins */
					int num_vertices;           /* number of vertices per frame */
					int num_st;                 /* number of texture coordinates */
					int num_tris;               /* number of triangles */
					int num_glcmds;             /* number of opengl commands */
					int num_frames;             /* number of frames */

					int offset_skins;           /* offset skin data */
					int offset_st;              /* offset texture coordinate data */
					int offset_tris;            /* offset triangle data */
					int offset_frames;          /* offset frame data */
					int offset_glcmds;          /* offset OpenGL command data */
					int offset_end;             /* offset end of file */
				};

				/* Texture name */
				struct md2_skin_t
				{
					char name[64];              /* texture file name */
				};

				/* Texture coords */
				struct md2_texCoord_t
				{
					short s;
					short t;
				};

				/* IndexedTriangle info */
				struct md2_triangle_t
				{
					unsigned short vertex[3];   /* vertex indices of the triangle */
					unsigned short st[3];       /* tex. coord. indices */
				};

				/* Compressed vertex */
				struct md2_vertex_t
				{
					unsigned char v[3];         /* position */
					unsigned char normalIndex;  /* normal vector index */
				};

				/* Model frame */
				struct md2_frame_t
				{
					vec3_t scale;               /* scale factor */
					vec3_t translate;           /* translation vector */
					char name[16];              /* frame name */
					struct md2_vertex_t *verts; /* list of frame's vertices */
				};

				/* GL command packet */
				struct md2_glcmd_t
				{
					float s;                    /* s texture coord. */
					float t;                    /* t texture coord. */
					int index;                  /* vertex index */
				};

			public:
				static AnimatedEntity *LoadFromFile(string sFilepath);
				static vec3_t anorms[162];
			};
		}
	}
}

#endif